
# 登录脚本返回session对象,其他脚本可以通过session对象进行后续操作
import requests,json,time
from CmdColorUtil import CmdColorUtil



url = "http://localhost:9000/userlogin"
header = {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Content-Type": "application/json",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                  "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
}


def soloLogin(userObj):
    t1 = time.time()
    print("************************登录脚本*********************")
    session = requests.session()
    req = session.post(url,data= json.dumps(userObj),headers = header)
    t2 = time.time()
    print("code:\n",req.status_code )
    print("响应内容:\n",req.text )
    try:
        PStoken = json.loads(req.text)["data"]["token"]
    except KeyError:
        CmdColorUtil.yellow("===KeyError!!!登录失败===")
        return

    CmdColorUtil.green("token:\n%s"%PStoken)
    session.headers["PStoken"] = PStoken
    t3 = time.time()
    print("登录耗时:\n",t2-t1)
    print("参数配置耗时:\n",t3-t2)
    return session


if __name__ == '__main__':
    soloLogin({
        "userName":2,
        "password":2*111
    })